<?php

namespace addons\qingdongams\controller;

use addons\qingdongams\model\Discuss as DiscussModel;
use addons\qingdongams\model\Message;

use think\Db;
use think\Exception;
/**
 * @desc 操作文档：https://doc.fastadmin.net/qingdongams
 * @desc 软件介绍：https://www.fastadmin.net/store/qingdongams.html
 * @desc 售后微信：qingdong_crm
 */
/**
 * 讨论
 */
class Discuss extends StaffApi
{
    protected $noNeedLogin = [];
    protected $noNeedRight = [];

    //添加讨论
    public function addDiscuss()
    {
        $title = input('title');
        $content = input('content');
        $file_ids = input('file_ids');
        $relation_id = input('relation_id');
        $relation_type = input('relation_type');
        $show_staff_id = input('show_staff_id');
        if (empty($content)) {
            $this->error('评论内容不能为空');
        }
        $data = [
            'relation_type' => $relation_type,
            'relation_id' => $relation_id,
            'create_staff_id' => $this->auth->id,
            'title' => $title,
            'file_ids' => $file_ids,
            'content' => $content,
            'show_staff_id' => $show_staff_id,
        ];


        $discussModel = new DiscussModel();
        Db::startTrans();
        try {
            $discussModel->save($data);
            $lastid=$discussModel->getLastInsID();
            $content2 = $this->auth->name . '发起《'.$title.'》讨论，邀请您参加!';

            $show_staff_id=explode(',',$show_staff_id);
            foreach ($show_staff_id as $staff_id) {
                //发送通知
                if ($staff_id != $this->auth->id) {
                    Message::addMessage(Message::DISCUSS_TYPE, $lastid, $staff_id, $this->auth->id, $content2);
                }
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('发起讨论成功');
    }


    //列表
    public function getList()
    {
        $relation_id = input('relation_id');
        $relation_type = input('relation_type');

        $comments = DiscussModel::where([
            'relation_type' => $relation_type,
            'relation_id' => $relation_id,
        ])->order('id desc')->with(['staff'])->paginate();
        $this->success('请求成功', $comments);
    }
    //详情
    public function getDetail()
    {
        $id = input('id');

        $discuss = DiscussModel::where([
            'id' => $id,
        ])->with(['staff'])->find();
        if(empty($discuss)){
            $this->error('讨论信息不存在');
        }
        $discuss['is_edit']=0;
        if($discuss['create_staff_id'] == $this->auth->id){
            $discuss['is_edit']=1;
        }
        Message::setRead(Message::DISCUSS_TYPE,$id,$this->auth->id);
        $this->success('请求成功', $discuss);
    }

    //修改讨论
    public function editDiscuss()
    {
        $id = input('id');
        $title = input('title');
        $content = input('content');
        $file_ids = input('file_ids');
        $show_staff_id = input('show_staff_id');
        if (empty($content)) {
            $this->error('评论内容不能为空');
        }
        $row=DiscussModel::get($id);
        if(empty($row)){
            $this->error('参数错误');
        }
        $save=[];
        if($title){
            $save['title']=$title;
        }
        if($file_ids){
            $save['file_ids']=$file_ids;
        }
        if($content){
            $save['content']=$content;
        }
        if($show_staff_id){
            $save['show_staff_id']=$show_staff_id;
        }
        $data = [
            'title' => $title,
            'file_ids' => $file_ids,
            'content' => $content,
            'show_staff_id' => $show_staff_id,
        ];
        $discussModel = new DiscussModel();
        Db::startTrans();
        try {
            $discussModel->save($data,['id'=>$id]);
            $content2 = $this->auth->name . '发起《'.$title.'》讨论，邀请您参加!';

            $show_staff_id=explode(',',$show_staff_id);
            foreach ($show_staff_id as $staff_id) {
                //发送通知
                if ($staff_id != $this->auth->id) {
                    Message::addMessage(Message::DISCUSS_TYPE, $id, $staff_id, $this->auth->id, $content2);
                }
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('修改讨论信息成功');
    }

}
